﻿@model BulkEditListModel
@inject AdminAreaSettings adminAreaSettings
@{
    //page title
    ViewBag.Title = T("Admin.Catalog.BulkEdit").Text;
}
<form asp-area="@Constants.AreaAdmin" asp-controller="Product" asp-action="BulkEdit" method="post">
    
    <div class="row">
        <div class="col-md-12">
            <div class="x_panel light form-fit">
                <div class="x_title">
                    <div class="caption level-caption">
                        <i class="fa fa-cubes"></i>
                        @T("Admin.Catalog.BulkEdit")
                    </div>
                    <vc:admin-widget widget-zone="product_bulk_edit_buttons" additional-data="null" />
                </div>
                <div class="x_content form">
                    <div class="form-horizontal">
                        <div class="form-body">
                            <div class="form-group">
                                <admin-label asp-for="SearchProductName" />
                                <div class="col-md-9 col-sm-9">
                                    <admin-input asp-for="SearchProductName" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchCategoryId" />
                                <div class="col-md-9 col-sm-9">
                                    <admin-select asp-for="SearchCategoryId" asp-items="Model.AvailableCategories" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchManufacturerId" />
                                <div class="col-md-9 col-sm-9">
                                    <admin-select asp-for="SearchManufacturerId" asp-items="Model.AvailableManufacturers" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchProductTypeId" />
                                <div class="col-md-9 col-sm-9">
                                    <admin-select asp-for="SearchProductTypeId" asp-items="Model.AvailableProductTypes" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchStoreId" />
                                <div class="col-md-9 col-sm-9">
                                    <admin-select asp-for="SearchStoreId" asp-items="Model.AvailableStores" />
                                </div>
                            </div>
                            <div class="form-actions">
                                <div class="row">
                                    <div class="offset-md-3 offset-sm-3 col-md-9 col-sm-9">
                                        <button class="btn btn-success filter-submit" id="search-products">
                                            <i class="fa fa-search"></i> @T("Admin.Common.Search")
                                        </button>
                                    </div>
                                </div>
                            </div>
                            <div class="x_content">
                                <div id="products-grid"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            $("#products-grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("BulkEditSelect", "Product"))",
                            type: "POST",
                            dataType: "json",
                            data: additionalData
                        },
                        update: {
                            url: "@Html.Raw(Url.Action("BulkEditUpdate", "Product"))",
                            type: "POST",
                            dataType: "json"
                        },
                        destroy: {
                            url: "@Html.Raw(Url.Action("BulkEditDelete", "Product"))",
                            type: "POST",
                            dataType: "json"
                        },
                        parameterMap: function(data, operation) {
                            if (operation != "read") {

                                var result = {};

                                for (var i = 0; i < data.models.length; i++) {
                                    var entity = data.models[i];

                                    for (var member in entity) {
                                        result["products[" + i + "]." + member] = entity[member];
                                        if (member == 'Price' || member == 'OldPrice') {
                                            result["products[" + i + "]." + member] = kendo.toString(entity[member], "n4");
                                        }
                                        
                                    }
                                }

                                addAntiForgeryToken(result);
                                return result;
                            } else {
                                addAntiForgeryToken(data);
                                //return JSON.stringify(data);
                                return data;
                            }
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors",
                        model: {
                            id: "Id",
                            fields: {
                                Name: { editable: true, type: "string" },
                                ProductId: { editable: false, type: "string" },
                                Sku: { editable: true, type: "string" },
                                Price: { editable: true, type: "number" },
                                OldPrice: { editable: true, type: "number" },
                                ManageInventoryMethod: { editable: false, type: "string" },
                                StockQuantity: { editable: true, type: "number" },
                                Published: { editable: true, type: "boolean" },
                                Id: { editable: false, type: "string" }
                            }
                        }
                    },
                    requestEnd: function (e) {
                        if (e.type == "update") {
                            this.read();
                        }
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    //bulk editing
                    batch: true,
                    pageSize: @(adminAreaSettings.DefaultGridPageSize),
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                pageable: {
                    refresh: true,
                    pageSizes: [@(adminAreaSettings.GridPageSizes)]
                },
                toolbar: ["save", "cancel"],
                editable: {
                    confirmation: true,
                    //bulk editing
                    mode: "incell"
                },
                scrollable: true,
                columns: [ {
                    field: "Name",
                    title: "@T("Admin.Catalog.BulkEdit.Fields.Name")",
                    width: 200
                }, {
                    field: "ProductId",
                    title: "@T("Admin.Common.View")",
                    width: 80,
                    template: '<a href="@Url.Action("Edit", "Product")/#=Id#">@T("Admin.Common.View")</a>'
                },{
                    field: "Sku",
                    title: "@T("Admin.Catalog.BulkEdit.Fields.Sku")",
                    width: 100
                }, {
                    field: "Price",
                    title: "@T("Admin.Catalog.BulkEdit.Fields.Price")",
                    width: 100,
                    editor: function (container, options) {
                        $('<input name="' + options.field + '"/>')
                            .appendTo(container)
                            .kendoNumericTextBox({
                                format: "{0:n4}",
                                decimals: 4
                            });
                    }
                }, {
                    field: "OldPrice",
                    title: "@T("Admin.Catalog.BulkEdit.Fields.OldPrice")",
                    width: 100,
                    editor: function (container, options) {
                        $('<input name="' + options.field + '"/>')
                            .appendTo(container)
                            .kendoNumericTextBox({
                                format: "{0:n4}",
                                decimals: 4
                            });
                    }
                }, @*{
                    field: "ManageInventoryMethod",
                    title: "@T("Admin.Catalog.BulkEdit.Fields.ManageInventoryMethod")",
                    width: 120
                },*@ {
                    field: "StockQuantity",
                    title: "@T("Admin.Catalog.BulkEdit.Fields.StockQuantity")",
                    //integer format
                    format: "{0:0}",
                    width: 100
                },{
                    field: "Published",
                    title: "@T("Admin.Catalog.BulkEdit.Fields.Published")",
                    width: 90
                }, {
                    command: { name: "destroy", text: "@T("Admin.Common.Delete")" },
                    title: "@T("Admin.Common.Delete")",
                    width: 100
                }
                ]
            } );
        });
    </script>

    <script>

        $(document).ready(function () {
            $('#search-products').click(function () {
                var grid = $('#products-grid').data('kendoGrid');
                grid.dataSource.page(1); //new search. Set page size to 1
                //grid.dataSource.read(); we already loaded the grid above using "page" function
                return false;
            });

            $("#@Html.FieldIdFor(model => model.SearchProductName)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#search-products").click();
                    return false;
                }
            });
        });

        function additionalData() {
            var data = {
                SearchProductName: $('#@Html.FieldIdFor(model => model.SearchProductName)').val(),
                SearchCategoryId: $('#SearchCategoryId').val(),
                SearchManufacturerId: $('#SearchManufacturerId').val(),
                SearchProductTypeId: $('#SearchProductTypeId').val(),
                SearchStoreId: $('#SearchStoreId').val(),
            };
            addAntiForgeryToken(data);
            return data;
        }
    </script>
</form>